/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
*/

package com.jinhe.tss.core.common.license;

import java.util.Date;

import com.jinhe.tss.core.ConfigurableContants;
import com.jinhe.tss.core.util.DateUtil;
import com.jinhe.tss.core.util.EasyUtils;

/** 
 * <p> License.java </p> 
 * 授权许可
 */
public final class License extends ConfigurableContants{
    
    String product;          //产品
    String version;          //版本号
    String macAddress;       //安装的服务器Mac地址
    Date   expiresDate;      //过期时间
    String licenseSignature; //license签名
    
    LicenseType licenseType; //license类型
    
    String getProduct() {
        return product;
    }
    String getVersion() {
        return version;
    }
    String getMacAddress() {
        return macAddress;
    }
    Date getExpiresDate() {
        return expiresDate;
    }
    String getLicenseSignature() {
        return licenseSignature;
    }
    LicenseType getLicenseType() {
        return licenseType;
    }
    
    /**
     * 把license的属性值拼成一个字符串，然后转身字节数组
     */
    public byte[] getFingerprint() {
        StringBuffer buf = new StringBuffer(100);
        buf.append(product).append(version).append(licenseType);
        if(macAddress != null)
            buf.append(macAddress);
        if(expiresDate != null)
            buf.append(DateUtil.format(expiresDate));
        return buf.toString().getBytes();
    }

    public String toString() {
        StringBuffer buf = new StringBuffer(100);
        buf.append("product=" + product + "\n");
        buf.append("version=" + version + "\n");
        buf.append("type=" + licenseType + "\n");
        if(expiresDate != null)
            buf.append("expiry=" + DateUtil.format(expiresDate) + "\n");
        if(macAddress != null && macAddress.length() > 0)
            buf.append("macaddress=" + macAddress + "\n");
        buf.append("signature=" + licenseSignature);
        return buf.toString();
    }

    /**
     * 读取许可文件
     * @param fileName
     */
    public static License fromConfigFile(String fileName) throws Exception {

        init(LicenseFactory.LICENSE_DIR + "/" + fileName);
        
        License license = new License();
        license.product = getProperty("product");
        license.version = getProperty("version");
        license.licenseSignature = getProperty("signature");
        
        String type = getProperty("type");
        if( !EasyUtils.isNullOrEmpty(type) ) {
            license.licenseType = new LicenseType(type);
        }
        
        String expiry = getProperty("expiry");
        if( !EasyUtils.isNullOrEmpty(expiry) ) {
            license.expiresDate = DateUtil.parse(expiry);
        }
        
        String macAddress = getProperty("macaddress");
        if( !EasyUtils.isNullOrEmpty(macAddress) ) {
            license.macAddress = macAddress;
        }
        
        return license;
    }
    
    /**
     * 授权许可类型。
     */
    static final class LicenseType {

        static final LicenseType NON_COMMERCIAL = new LicenseType("Non-Commercial"); //非商业的
        static final LicenseType COMMERCIAL = new LicenseType("Commercial");  // 商业的
        static final LicenseType EVALUATION = new LicenseType("Evaluation"); // 评测的
        static final LicenseType ADVANCED   = new LicenseType("Advanced");  //高级的

        final String name;
        
        LicenseType(String name) {
            this.name = name;
        }

        public String toString()  {
            return name;
        }

        public static LicenseType fromString(String type) {
            if(COMMERCIAL.name.equals(type))
                return COMMERCIAL;
            if(EVALUATION.name.equals(type))
                return EVALUATION;
            if(ADVANCED.name.equals(type))
                return ADVANCED;
            else
                return NON_COMMERCIAL;
        }
    }
}